//
//  JSMCollectionOfMessage.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/7.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMCollectionOfMessage.h"

@implementation JSMCollectionOfMessage

+ (void)isHaveModelFolder{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *modelFilePath = [sandBoxPath stringByAppendingPathComponent:@"TYTT"];
    //创建管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //通过管理者调用方法,根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:modelFilePath]) {
        [manager createDirectoryAtPath:modelFilePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

#pragma mark PLIST
+ (NSString *)pathOfType:(CollectionOfMessageType)type{
    NSString *sandPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path;
    if (type == CollectionOfMessageTypeLeavingModel ) {
        path = [sandPath stringByAppendingPathComponent:@"TYTT/leaving.plist"];
    } else if (type == CollectionOfMessageTypeDreamCitySpecialModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/dreamCitySpecial.plist"];
    }else if (type == CollectionOfMessageTypeDreamCityPrefectureModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/dreamCityPrecial.plist"];
    }else if (type == CollectionOfMessageTypeTouringModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/touring.plist"];
    }else if (type == CollectionOfMessageTypeStrategyModel){
        path = [sandPath stringByAppendingPathComponent:@"TYTT/strategy.plist"];
        
    }
    
    return path;
}

#pragma mark 保存缓存
+ (void)saveModelToPlist:(NSMutableArray *)array type:(CollectionOfMessageType)type {
    [self isHaveModelFolder];
    NSString *path = [self pathOfType:type];
    NSLog(@"%@",path);
    //先反
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (arr == nil) {
        //如果为空先进行初始化
        arr = [NSMutableArray array];
    }
    [arr removeAllObjects];
    arr = array;
    //归档
    [NSKeyedArchiver archiveRootObject:arr toFile:path];
}

#pragma mark 取出缓存
+ (NSMutableArray *)getModelToPlistType:(CollectionOfMessageType)type{
    [self isHaveModelFolder];
    NSString *path = [self pathOfType:type];
    NSLog(@"%@",path);
    //先反
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (arr == nil) {
        //如果为空先进行初始化
        arr = [NSMutableArray array];
    }
    return arr;
}

@end
